package com.xzmzhp.controller.activity;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.activity.ProgramLibrary;
import com.xzmzhp.service.activity.ProgramLibraryService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-04 10:27
 * @program: ams
 * @description: 节目库前端交互类
 */

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/programLibrary")
public class ProgramLibraryController {

    @Autowired
    private ProgramLibraryService programLibraryService;

    /**
     * 查询所有节目数据
     * @return 节目库数据
     */
    @RequestMapping("/findAll")
    public ReturnData findAll(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "limit", defaultValue = "10") int limit){
        //开启分页
        PageHelper.startPage(page, limit, true);
        List<ProgramLibrary> list = programLibraryService.findAll();
        if (CollUtil.isNotEmpty(list)){
            PageInfo<ProgramLibrary> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询节目库信息失败,请重新查询");
    }

    /**
     * 根据参数查询数据
     * @param param 参数
     *        programName 节目名称
     *        programTypeId 节目类型Id
     *        programTypeName 节目类型名称
     *        startTime 开始时间
     *        endTime 结束时间
     * @return 节目库数据
     */
    @RequestMapping("/findProgramLibraryByParam")
    public ReturnData findProgramLibraryByParam(@RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "limit", defaultValue = "10") int limit,
                                                @RequestBody Map<String,Object> param){
        PageHelper.startPage(page, limit, true);
        List<ProgramLibrary> list = programLibraryService.findProgramLibraryByParam(param);
        if (CollUtil.isNotEmpty(list)){
            PageInfo<ProgramLibrary> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询活动信息失败,请重新查询");
    }

    /**
     * 根据id查询节目内容
     * @param programId 节目id
     * @return 节目内容
     */
    @RequestMapping("/findContentById")
    public ReturnData findContentById(@RequestParam(value = "programId") Integer programId){
        String str = programLibraryService.findContentById(programId);
        if (StringUtils.isNotNull(str)) {
            return ReturnData.ok(str);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "查询节目内容信息失败,请重新查询");
        }
    }

    /**
     * 根据节目id查询节目信息
     * @param programId 节目信息
     * @return 节目信息
     */
    @RequestMapping("/findProgramLibraryById")
    public ReturnData findProgramLibraryById(@RequestParam(value = "programId") Integer programId){
        ProgramLibrary programLibrary = programLibraryService.findProgramLibraryById(programId);
        if (StringUtils.isNotNull(programLibrary)) {
            return ReturnData.ok(programLibrary);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "查询节目信息失败,请重新查询");
        }
    }

    /**
     * 根据节目名称查询节目信息
     * @param programName 节目名称
     * @return 节目信息
     */
    @RequestMapping("/findProgramLibraryByProgramName")
    public ReturnData findProgramLibraryByProgramName(@RequestParam(value = "programName") String programName){
        ProgramLibrary programLibrary = programLibraryService.findProgramLibraryByProgramName(programName);
        if (StringUtils.isNotNull(programLibrary)) {
            return ReturnData.ok(programLibrary);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "查询节目信息失败,请重新查询");
        }
    }

    /**
     * 添加节目库信息
     * @param programLibrary 节目库信息
     * @return 是否成功
     */
    @RequestMapping("/addProgramLibrary")
    public ReturnData addProgramLibrary(@RequestBody ProgramLibrary programLibrary){
        Integer i = programLibraryService.addProgramLibrary(programLibrary);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "节目信息添加错误");
        }
    }

    /**
     * 修改节目信息
     * @param programLibrary 节目库信息
     * @return 是否成功
     */
    @RequestMapping("/exitProgramLibrary")
    public ReturnData exitProgramLibrary(@RequestBody ProgramLibrary programLibrary){
        Integer i = programLibraryService.exitProgramLibrary(programLibrary);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "节目信息修改错误");
        }
    }

    /**
     * 根据节目id删除节目信息(仅允许未使用的节目被删除)
     * @param programId 节目id
     * @return 是否成功
     */
    @RequestMapping("/programId")
    public ReturnData delProgramLibrary(@RequestParam(value = "programId") Integer programId){
        Integer i = programLibraryService.delProgramLibrary(programId);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "节目删除修改错误");
        }
    }
}
